import request from "@/utils/request"; // axios请求

import type {
  IBaseSaleAttrList,
  ITrademarkList,
  ISpuSaleAttrList,
  ISpuImageList,
  ISpuDataList,
  ISpuData,
  IAttrList,
  IAddSkuParams,
  ISkuList,
} from "./model"; // 数据类型

enum API {
  SPULIST = "/admin/product/", // spu数据列表的接口
  BASESALEATTRLIST = "/admin/product/baseSaleAttrList", // 基础销售属性数据列表的接口
  TRADEMARKLIST = "/admin/product/baseTrademark/getTrademarkList", // 品牌数据列表的接口
  SPUSALEATTRLIST = "/admin/product/spuSaleAttrList/", // spu销售属性列表的接口
  SPUIMAGELIST = "/admin/product/spuImageList/", // spu图片数据列表的接口
  ADDSPU = "/admin/product/saveSpuInfo", // 添加spu
  UPDATESPU = "/admin/product/updateSpuInfo", // 更新spu
  ATTRINFOLIST = "/admin/product/attrInfoList/", // 商品基础属性列表
  ADDSKU = "/admin/product/saveSkuInfo", // 添加SKU
  LOOKSKU = "/admin/product/findBySpuId/", // 查看SKU
  DELETESPU = "/admin/product/deleteSpu/", // 删除SKU
}

// 获取SPU数据列表
export const reqSpuList = (
  page: number,
  limit: number,
  category3Id: number | string
) =>
  request.get<any, ISpuDataList>(API.SPULIST + `${page}/${limit}`, {
    params: {
      category3Id,
    },
  });

// 请求基础销售属性数据列表
export const reqBaseSaleAttrList = () =>
  request.get<any, IBaseSaleAttrList>(API.BASESALEATTRLIST);

// 请求品牌列表数据
export const reqTrademarkList = () =>
  request.get<any, ITrademarkList>(API.TRADEMARKLIST);

// 请求spu销售属性列表
export const reqSpuSaleAttrList = (spuId: number) =>
  request.get<any, ISpuSaleAttrList>(API.SPUSALEATTRLIST + spuId);

// 请求spu图片列表
export const reqSpuImageList = (spuId: number) =>
  request.get<any, ISpuImageList>(API.SPUIMAGELIST + spuId);

// 添加或修改spu
export const reqAddOrUpdateSpu = (data) => {
  if (data.id) {
    return request.post(API.UPDATESPU, data);
  } else {
    return request.post(API.ADDSPU, data);
  }
};

// 请求商品基础属性
export const reqAttrInfoList = (
  category1Id: string | number,
  category2Id: string | number,
  category3Id: string | number
) =>
  request.get<any, IAttrList>(
    API.ATTRINFOLIST + `${category1Id}/${category2Id}/${category3Id}`
  );

// 添加SKU
export const reqAddSku = (data: IAddSkuParams) =>
  request.post<any, any>(API.ADDSKU, data);

// 请求SKU数据
export const reqLookSku = (spuId: number | string | undefined) =>
  request.get<any, ISkuList>(API.LOOKSKU + spuId);

// 删除spu
export const reqDeleteSpu = (spuId: number | string | undefined) =>
  request.delete<any, any>(API.DELETESPU + spuId);
